package com.xh3140.android.widgetdemo

import android.graphics.Color
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.xh3140.android.widget.dialog.ContentDialog
import com.xh3140.android.widget.dialog.InputDialog
import com.xh3140.android.widget.dialog.ListDialog
import com.xh3140.android.widget.dialog.LoadingDialog
import com.xh3140.android.widget.dialog.values.ColorRes
import com.xh3140.android.widget.toast.Toast
import com.xh3140.android.widgetdemo.databinding.ActivityDialogBinding

class DialogActivity : AppCompatActivity() {

    private val binding: ActivityDialogBinding by lazy {
        DataBindingUtil.setContentView(this, R.layout.activity_dialog)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        initListener()
    }

    private fun initListener() {
        val changHenGe = "汉皇重色思倾国，御宇多年求不得。\n" + "\n" +
                "杨家有女初长成，养在深闺人未识。\n" + "\n" +
                "天生丽质难自弃，一朝选在君王侧。\n" + "\n" +
                "回眸一笑百媚生，六宫粉黛无颜色。\n" + "\n" +
                "春寒赐浴华清池，温泉水滑洗凝脂。\n" + "\n" +
                "侍儿扶起娇无力，始是新承恩泽时。\n" + "\n" +
                "云鬓花颜金步摇，芙蓉帐暖度春宵。\n" + "\n" +
                "春宵苦短日高起，从此君王不早朝。\n" + "\n" +
                "承欢侍宴无闲暇，春从春游夜专夜。\n" + "\n" +
                "后宫佳丽三千人，三千宠爱在一身。\n" + "\n" +
                "金屋妆成娇侍夜，玉楼宴罢醉和春。\n" + "\n" +
                "姊妹弟兄皆列土，可怜光彩生门户。\n" + "\n" +
                "遂令天下父母心，不重生男重生女。\n" + "\n" +
                "骊宫高处入青云，仙乐风飘处处闻。\n" + "\n" +
                "缓歌慢舞凝丝竹，尽日君王看不足。\n" + "\n" +
                "渔阳鼙鼓动地来，惊破霓裳羽衣曲。\n" + "\n" +
                "九重城阙烟尘生，千乘万骑西南行。\n" + "\n" +
                "翠华摇摇行复止，西出都门百余里。\n" + "\n" +
                "六军不发无奈何，宛转蛾眉马前死。\n" + "\n" +
                "花钿委地无人收，翠翘金雀玉搔头。\n" + "\n" +
                "君王掩面救不得，回看血泪相和流。\n" + "\n" +
                "黄埃散漫风萧索，云栈萦纡登剑阁。\n" + "\n" +
                "峨嵋山下少人行，旌旗无光日色薄。\n" + "\n" +
                "蜀江水碧蜀山青，圣主朝朝暮暮情。\n" + "\n" +
                "行宫见月伤心色，夜雨闻铃肠断声。\n" + "\n" +
                "天旋地转回龙驭，到此踌躇不能去。\n" + "\n" +
                "马嵬坡下泥土中，不见玉颜空死处。\n" + "\n" +
                "君臣相顾尽沾衣，东望都门信马归。\n" + "\n" +
                "归来池苑皆依旧，太液芙蓉未央柳。\n" + "\n" +
                "芙蓉如面柳如眉，对此如何不泪垂。\n" + "\n" +
                "春风桃李花开夜，秋雨梧桐叶落时。\n" + "\n" +
                "西宫南苑多秋草，落叶满阶红不扫。\n" + "\n" +
                "梨园弟子白发新，椒房阿监青娥老。\n" + "\n" +
                "夕殿萤飞思悄然，孤灯挑尽未成眠。\n" + "\n" +
                "迟迟钟鼓初长夜，耿耿星河欲曙天。\n" + "\n" +
                "鸳鸯瓦冷霜华重，翡翠衾寒谁与共。\n" + "\n" +
                "悠悠生死别经年，魂魄不曾来入梦。\n" + "\n" +
                "临邛道士鸿都客，能以精诚致魂魄。\n" + "\n" +
                "为感君王辗转思，遂教方士殷勤觅。\n" + "\n" +
                "排空驭气奔如电，升天入地求之遍。\n" + "\n" +
                "上穷碧落下黄泉，两处茫茫皆不见。\n" + "\n" +
                "忽闻海上有仙山，山在虚无缥渺间。\n" + "\n" +
                "楼阁玲珑五云起，其中绰约多仙子。\n" + "\n" +
                "中有一人字太真，雪肤花貌参差是。\n" + "\n" +
                "金阙西厢叩玉扃，转教小玉报双成。\n" + "\n" +
                "闻道汉家天子使，九华帐里梦魂惊。\n" + "\n" +
                "揽衣推枕起徘徊，珠箔银屏迤逦开。\n" + "\n" +
                "云鬓半偏新睡觉，花冠不整下堂来。\n" + "\n" +
                "风吹仙袂飘飖举，犹似霓裳羽衣舞。\n" + "\n" +
                "玉容寂寞泪阑干，梨花一枝春带雨。\n" + "\n" +
                "含情凝睇谢君王，一别音容两渺茫。\n" + "\n" +
                "昭阳殿里恩爱绝，蓬莱宫中日月长。\n" + "\n" +
                "回头下望人寰处，不见长安见尘雾。\n" + "\n" +
                "惟将旧物表深情，钿合金钗寄将去。\n" + "\n" +
                "钗留一股合一扇，钗擘黄金合分钿。\n" + "\n" +
                "但教心似金钿坚，天上人间会相见。\n" + "\n" +
                "临别殷勤重寄词，词中有誓两心知。\n" + "\n" +
                "七月七日长生殿，夜半无人私语时。\n" + "\n" +
                "在天愿作比翼鸟，在地愿为连理枝。\n" + "\n" +
                "天长地久有时尽，此恨绵绵无绝期。 "

        binding.buttonContentDialog.setOnClickListener {
            ContentDialog.Builder(this, 1)
                .setTitleText("长恨歌")
                .setSubtitleText("白居易")
                .setContentText(changHenGe)
                .setButtonText(0, "朕已阅")
                .build()
                .show()
        }

        binding.buttonInputDialog.setOnClickListener {
            InputDialog.Builder(this, 2)
                .setTitleText("你的名字")
                .setInputText("张三")
                .setInputHint("爪巴")
                .setButtonsText("取消", "确定")
                .setButtonTextColor(0, ColorRes.BUTTON_NEGATIVE_TEXT_COLOR)
                .setButtonOnClickListener { dialog, i, _ ->
                    if (i == 1) {
                        val text = dialog.inputText?.toString()
                        if (text.isNullOrEmpty()) {
                            Toast.normal(this, "没有输入").show()
                        } else {
                            Toast.normal(this, "你的名字是:${dialog.inputText?.toString()}").show()
                        }
                    }
                    dialog.dismiss()
                }
                .build()
                .show()
        }

        val provinces = listOf(
            "北京市", "重庆市", "天津市", "上海市",
            "广东省", "山东省", "江苏省", "河南省", "河北省", "浙江省", "陕西省", "湖南省", "福建省",
            "云南省", "四川省", "安徽省", "海南省", "江西省", "湖北省", "山西省", "辽宁省", "台湾省", "贵州省",
            "甘肃省", "青海省", "黑龙江省", "吉林省",
            "香港特别行政区", "澳门特别行政区",
            "内蒙古自治区", "新疆维吾尔自治区", "西藏自治区", "广西壮族自治区", "宁夏回族自治区"
        )
        binding.buttonListSingleDialog.setOnClickListener {
            ListDialog.Builder(this, 2, provinces.size)
                .setTitleText("你的居住地")
                .setSubtitleText("中华人民共和国行政区划")
                .setListItemsText(0, provinces)
                .setButtonsText("取消", "确定")
                .setButtonTextColor(0, ColorRes.BUTTON_NEGATIVE_TEXT_COLOR)
                .setButtonOnClickListener { dialog, i, _ ->
                    if (i == 1) {
                        val index = dialog.checkedIndex
                        if (index >= 0) {
                            Toast.normal(this, "你居住在:${provinces[index]}").show()
                        } else {
                            Toast.normal(this, "没有选中任何项目").show()
                        }
                    }
                    dialog.dismiss()
                }
                .build()
                .show()
        }

        val fruits = listOf("苹果", "香蕉", "橘子", "葡萄", "菠萝", "桃子", "榴莲")
        binding.buttonListMultipleDialog.setOnClickListener {
            ListDialog.Builder(this, 2, fruits.size)
                .setTitleText("你喜欢的水果有？")
                .setListItemsText(listOf(0, 1, 2), fruits)
                .setButtonsText("取消", "确定")
                .setButtonTextColor(0, ColorRes.BUTTON_NEGATIVE_TEXT_COLOR)
                .setButtonOnClickListener { dialog, i, _ ->
                    if (i == 1) {
                        val indexList = dialog.checkedIndexList
                        val list = mutableListOf<String>()
                        for (index in indexList) {
                            list.add(fruits[index])
                        }
                        if (list.isNotEmpty()) {
                            Toast.normal(this, "你喜欢的水果有:${list}").show()
                        } else {
                            Toast.normal(this, "没有选中任何项目").show()
                        }
                    }
                    dialog.dismiss()
                }
                .build()
                .show()
        }

        binding.buttonLoadingSpinnerDialog.setOnClickListener {
            LoadingDialog.Builder(this, 0)
                .setLoadingSpinner(true)
                .setLoadingSubtitleText("加载中...")
                .build()
                .show()
        }

        binding.buttonLoadingHorizontalDialog.setOnClickListener {
            LoadingDialog.Builder(this, 0)
                .setTitleText("等待对话框")
                .setLoadingSpinner(false)
                .setLoadingSubtitleText("加载中...")
                .setLoadingTintColor(Color.RED)
                .build()
                .show()
        }
    }
}